<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>login</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<style type="text/css">
			header.mui-bar .mui-icon {
				font-size: 18px;
				padding-top: 13px;
				padding-bottom: 13px;
				color: #333;
			}

			.area {
				margin: 20px auto 0px auto;
			}

			.mui-input-group label {
				width: 20%;
			}

			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 80%;
			}

			.mui-input-row {
				position: relative;
			}

			.mui-input-row a {
				display: inline-block;
				position: absolute;
				right: 12px;
				line-height: 60px;
				font-size: 18px;
			}

			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}

			.mui-content-padded {
				margin-top: 25px;
			}

			.mui-btn {
				padding: 10px;
			}

			.mui-input-group .mui-input-row {
				height: 60px;
			}

			input[type=color],
			input[type=date],
			input[type=datetime-local],
			input[type=datetime],
			input[type=email],
			input[type=month],
			input[type=number],
			input[type=password],
			input[type=search],
			input[type=tel],
			input[type=text],
			input[type=time],
			input[type=url],
			input[type=week],
			select,
			textarea {
				height: 60px;
			}

			.mui-input-row label {
				height: 60px;
				font-size: 20px;
				line-height: 40px;
			}

			.mui-segmented-control .mui-control-item {
				line-height: 50px;
				font-size: 18px;
			}

			.mui-segmented-control.mui-segmented-control-inverted .mui-control-item {
				color: gray;
			}

			.mui-segmented-control.mui-segmented-control-inverted .mui-control-item.mui-active {
				color: #333;
			}

			.mui-slider .mui-segmented-control.mui-segmented-control-inverted~.mui-slider-group .mui-slider-item {
				border-bottom: 0;
			}

			.mui-content-padded {
				text-align: center;
				font-size: 18px;
			}

			.mui-content-padded p {
				font-size: 18px;
			}

			.mui-content-padded p a {
				text-decoration: underline;
				color: #333;
			}

			#item1 .mui-content-padded a {
				color: #333;
			}

			.mui-segmented-control.mui-segmented-control-inverted~.mui-slider-progress-bar {
				background-color: orange;
			}

			.mui-input-row .mui-input-clear~.mui-icon-clear,
			.mui-input-row .mui-input-password~.mui-icon-eye,
			.mui-input-row .mui-input-speech~.mui-icon-speech {
				top: 20px;
			}
		</style>
	</head>

	<body>
		<script src="js/mui.min.js"></script>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-icon mui-pull-left">登录</a>
			<a class="mui-icon mui-pull-right" id="phone_reg">注册</a>
		</header>
		<div class="mui-content">
			<div class="mui-slider">
				<div class="mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
					<a class="mui-control-item mui-active" href="#item1">账号密码登录</a>
					<a class="mui-control-item" href="#item2">手机号快捷登录</a>
				</div>
				<div id="sliderProgressBar" class="mui-slider-progress-bar mui-col-xs-6"></div>
				<div class="mui-slider-group">
					<div id="item1" class="mui-slider-item mui-control-content mui-active">
						<!-- 账号密码登录 -->
						<form class="mui-input-group">
							<div class="mui-input-row">
								<label>账号</label>
								<input id='item1phone' type="tel" class="mui-input-clear mui-input" placeholder="请输入账号">
							</div>
							<div class="mui-input-row">
								<label>密码</label>
								<input id='item1password' type="password" class="mui-input-password mui-input" placeholder="请输入密码">
								<span class="mui-icon mui-icon-eye"></span>
							</div>
							<div class="mui-content-padded">
								<button class="mui-btn mui-btn-block mui-btn-primary" id="index">登录</button>
							</div>
						</form>
						<div class="mui-content-padded">
							<div class="link-area">
								<a id='phone_reg'>注册账号</a> <span class="spliter">|</span>
								<a id='forgetPassword'>忘记密码?</a>
							</div>
						</div>
					</div>
					<div id="item2" class="mui-slider-item mui-control-content">
						<!-- 手机号快捷登录 -->
						<form class="mui-input-group">
							<div class="mui-input-row">
								<label>手机号</label>
								<input id='item2phone' type="number" class="mui-input-clear mui-input" placeholder="请输入手机号" oninput="if(value.length>11)value=value.slice(0,11)">
							</div>
							<div class="mui-input-row">
								<label>验证码</label>
								<input id='item2password' type="text" class="mui-input-clear mui-input" placeholder="请输入短信验证码">
								<a href="javascript:;" data-disabled='false'>获取验证码</a>
							</div>
							<div class="mui-content-padded">
								<button class="mui-btn mui-btn-block mui-btn-primary">登录</button>
							</div>
						</form>
						<div class="mui-content-padded">
							<p>登录代表你已同意
								<a id="xieyi">《MeiTuan用户协议》</a>
							</p>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
	<script src="js/zepto_1.1.3.js" type="text/javascript" charset="utf-8"></script>
	<script src="js/server.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		mui.init();
		mui('.mui-input-row').on('tap', 'a[data-disabled="false"]', function(event) {
			event.stopPropagation();
			var phone = document.getElementById("item2phone").value;
			console.log(phone)
			console.log(server)

			if(!(RegPhone.test(phone))){ // false
				// mui.toast('请输入正确的手机号')
				mui.toast('亲~您的火星通讯码,地球人不认识~')
				return false;
			}
			mui.ajax(server + '/sms/vcode', {
				data: {
					phonenumber: phone
				},
				dataType: "json",
				type: 'post',
				timeout: 10000,
				success: function(data) {
					console.log(data)
					if(data.status == 500) {
						mui.toast(data.msg);
						mui('.mui-input-row a')[0].innerHTML = '获取验证码';
						mui('.mui-input-row a')[0].setAttribute('data-disabled', 'false');
						clearTimeout(timer);
					} else if (data.status == 200){
						mui.toast('发送成功');
						var time = 60;
						(function timeD() {
							var timer = setTimeout(function() {
								if(time < 0) {
									mui('.mui-input-row a')[0].innerHTML = '获取验证码';
									mui('.mui-input-row a')[0].setAttribute('data-disabled', 'false');
									return false;
								} else {
									mui('.mui-input-row a')[0].innerHTML = time + 's 后再发送';
									mui('.mui-input-row a')[0].setAttribute('data-disabled', 'true');
									timeD();
								}
								time--;
							}, 1000)
						})();
					}
				},
				error: function(type) {
					mui.toast('验证码发送失败！请稍后重试')
				}
			})

		})

		var Tap = function(dom) {
			mui('body').on('tap', dom, function() {
					var phone = '', // 电话号码
						password = ''; // 密码、验证码
					if($('#item1').hasClass('mui-active')) {
						serverUrl = server + '/pwdlogin';
						phone = document.getElementById("item1phone").value;
						password = document.getElementById("item1password").value;
					} else {
						serverUrl = server + '/vcodelogin';
						phone = document.getElementById("item2phone").value;
						password = document.getElementById("item2password").value;
					}

					if(dom == 'button') {
						console.log(serverUrl)
						console.log(phone)
						console.log(password)
						mui.ajax(serverUrl, {
							data: {
								phonenumber: phone,
								code: password
							},
							dataType: "json",
							type: 'post',
							timeout: 10000,
							success: function(data) {
								console.log(data.status);
								console.log(data.msg)
								if(data.status == 500) {
									mui.toast(data.msg)
								} else if(data.status == 200) {
									// 跳转到首页
									mui.plusReady(function() {
										var current = plus.webview.currentWebview();
										var main = plus.webview.getLaunchWebview(); //获取首页对象
										mui.fire(main, "gohome", {
											index: 0
										}); //触发首页的 pageflowrefresh 方法
										// 关闭页面写在自定义函数下面   可以解决  关闭login后 → 显示的是用户信息页面 → 显示的是首页
										plus.webview.close(current, 'pop-out');
									})
								}

							},
							error: function(type) {
								mui.toast('登录失败！请检查设备网络链接状态')
							}
						})
					} else {
						var idUrl = this.getAttribute('id');
						if(idUrl) {
							var newUrl = idUrl + ".html";
							var indexWebview = mui.openWindow({
								url: newUrl,
								id: newUrl,
								createNew: false, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
								show: {
									autoShow: true,
									aniShow: "auto"
								},
								waiting: {
									autoShow: true,
									title: "正在加载..."
								}
							})
						}
					}
				})
		}
		Tap("header a");
		Tap("button");
	</script>

</html>